package com.yiwyn.demo.event;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;
import org.springframework.stereotype.Component;

@Slf4j(topic = "事务监听器")
@Component
public class MyTransactionEventListener {


    @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)  // 事务提交后触发
    public void handleTransactionCommit(MyTransactionEvent event) {
        log.info("事务提交:{}", event.getMessage());
    }

    @TransactionalEventListener(phase = TransactionPhase.AFTER_ROLLBACK)  // 事务回滚后触发
    public void handleTransactionRollback(MyTransactionEvent event) {
        System.out.println("Transaction rolled back: " + event.getMessage());
        log.error("事务进行了回滚:{}", event.getMessage());
    }
}
